package com.cuit.riskassessment.action.isms;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.cuit.riskassessment.service.AreaService;
import com.cuit.riskassessment.service.FocManageService;
import com.cuit.riskassessment.service.NewsManageService;
import com.cuit.riskassessment.util.JsonUtil;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.util.Tree;
import com.cuit.riskassessment.vo.Area;
import com.cuit.riskassessment.vo.FocManage;
import com.cuit.riskassessment.vo.News;

/**
 * Area 操作的相关action
 * 
 * @author Yang_Zhiqin-pc
 * 
 */
@Controller
@RequestMapping(value = "news")
public class NewsManageAction {
	final static private String SUCCESS = "success";
	final static private String MESSAGE = "message";
	@Autowired
	@Resource(name = "newsService")
	private NewsManageService newsService;
	private Map<String, Object> res;

	public NewsManageAction() {
		super();
		res = new HashMap<String, Object>();
	}

	public void writer(HttpServletResponse response, Map map) {
		response.setHeader("Accept", "text/html");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		PrintWriter writer = null;
		try {
			writer = response.getWriter();

			writer.print(JsonUtil.map2json(map));

			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			writer.close();
		}
	}

	@RequestMapping(value = "/addNews", method = RequestMethod.POST)
	public @ResponseBody
	void addFile(HttpServletResponse response, HttpServletRequest request,
			@RequestParam("title") String title, // 设置请求参数的名称和类型
			@RequestParam("authorId") Integer id, // 对应发布者的ID
			@RequestParam("type") boolean type, // 设置请求参数的名称和类型
			@RequestParam("isStickie") boolean isStickie, // 设置请求参数的名称和类型
			@RequestParam("appendix") CommonsMultipartFile mFile) { // 请求参数一定要与form中的参数名对应
		News news = new News();
		news.setId(id);
		news.setTitle(title);
		news.setType(type);
		news.setStickie(isStickie);
		if (mFile.isEmpty())
			news.setHaveAppendix(false);
		else
			news.setHaveAppendix(true);
		String path = mFile.getOriginalFilename();
		String pa = request.getRealPath("/");

		res.clear();
		res.put(MESSAGE, "操作失败！");
		res.put(SUCCESS, false);
		File filePath = new File(pa + "uploadFiles");
		if (!filePath.exists()) {
			filePath.mkdir();
		}
		try {
			FileCopyUtils.copy(mFile.getBytes(), new File(pa + "uploadFiles\\"
					+ path));
		} catch (IOException e) {
			e.printStackTrace();
			res.put(MESSAGE, "文件写入错误");
			writer(response, res);
			return;
		}
		if (this.newsService.addNews(news)) {
			res.put(MESSAGE, "添加成功");
			res.put(SUCCESS, true);
		}
		writer(response, res);
		return;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Object list(HttpServletResponse response,
			@RequestParam(value = "start") Integer start,
			@RequestParam(value = "limit") Integer limit) {
		response.setHeader("Accept", "application/json");
		res.clear();
		res.put("message", "数据读取错误，可能没开服务器哦");
		res.put("success", false);
		List<News> list = newsService.page(start, limit);
		if (list != null) {
			res.put("success", true);
			res.put("root", list);
			res.put("totalCount", newsService.getTotalCount());
			res.put("message", "读取成功");
		}
		return res;
	}

	@RequestMapping(value = "/delNews")
	public @ResponseBody
	Object del(@RequestParam(value = "id") int[] ids) {
		newsService.delNews(ids);
		res.clear();
		res.put("success", true);
		res.put("message", "删除成功");
		return res;
	}

	@RequestMapping(value = "/edit")
	public @ResponseBody
	Object del(News news) {
		res.clear();
		res.put("success", false);
		res.put("message", "删除失败");
		if (newsService.update(news)) {
			res.put("success", true);
			res.put("message", "删除成功");
		}
		return res;
	}
}
